<?php

namespace app\home\controller;

use app\home\model\User;
use think\cache\driver\Redis;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use think\Session;


class Home extends Controller
{
    public function login()
    {
        return view();
    }

    /**
     *
     * @return \think\response\Json
     */
    public function dologin()
    {
        $data = input();

//        验证
        $validate = Loader::validate('Goods');
        if (!$validate->scene('user')->check($data)) {
            $this->error($validate->getError(), '/home/home/login');
        }

        $res = User::login($data);

        if (isset($res)) {
//            生成token
            $token = \Token::create_token($res['id']);
            Session::set('token', $token);
            $data['token'] = $token;
            Session::set('data', $data);
            return json(['code' => 200, 'msg' => 'ok', 'data' => $data]);
        } else {
            return json(['code' => 500, 'msg' => '登录失败']);
        }
    }

//    软删除
    public function del($id)
    {
        $res = \app\home\model\Show::del($id);
        if (isset($res)) {
            $token = Session::get('token');
            $this->success('删除成功，正在返回展示页面...', 'home/Show/show?token=' . $token);
        } else {
            $this->success('删除失败，正在返回展示页面...', 'home/Show/show');
        }
    }

//    回收站
    public function recycling()
    {
        $word = input('word');
        $data = \app\home\model\Show::recycling($word);
        $this->assign('data', $data);
        return view();
    }

    //彻底删除
    public function delete($id)
    {

        $res = \app\home\model\Show::deletes($id);
        if (isset($res)) {
            //生成token
            $token = Session::get('token');
            $this->success('彻底删除成功，正在返回回收站...', '/home/home/recycling?token=' . $token);
        }
    }

    //数据恢复
    public function restore($id)
    {
        $res = \app\home\model\Show::restores($id);
        if (!isset($ree)) {
            //生成token
            $token = Session::get('token');
            $this->success('数据恢复成功', '/home/home/recycling?token=' . $token);
        } else {
            $this->success('数据恢复失败');
        }
    }

    //批量删除
    public function checkDel($id)
    {
        $res = \app\home\model\Show::deletes($id);
        if ($res) {
            return json(['code' => 200, 'successs' => '删除成功']);
        } else {
            return json(['code' => 501, 'successs' => '网络错误']);
        }
    }
}
